package dji.sampleV5.aircraft.upload

import android.content.Context
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import dji.v5.utils.common.LogUtils

class UploadWork(
    private val appContext: Context,
    params: WorkerParameters
): CoroutineWorker(appContext, params) {
    override suspend fun doWork(): Result {
        inputData.getStringArray(KEY_PATH_DATA)?.let {
            netManager.uploadFiles(NetConstants.URL_UAV_IMAGE, it)
        }
        return Result.success()
    }

    companion object {
        private const val KEY_PATH_DATA: String = "path_data"
        private val netManager = NetManager()
        fun startWork(context: Context, filePathList: List<String>) {
            val tag = filePathList.toString()
            val inputData = Data.Builder()
                .putStringArray(KEY_PATH_DATA, filePathList.toTypedArray())
                .build()
            val request = OneTimeWorkRequest.Builder(UploadWork::class.java)
                .addTag(tag)
                .setInputData(inputData)
                .build()
            WorkManager.getInstance(context)
                .beginUniqueWork(tag, ExistingWorkPolicy.KEEP, request)
                .enqueue()
        }
    }
}